package main

import (
	commandDemo "app/internal/command/demo"
	"app/wd"
	"context"
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var ctx context.Context
var rootCmd = &cobra.Command{
	Use:   "root",
	Short: "",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Hello, Cobra!")
	},
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		info := fmt.Sprintf("start %s args:%+v", cmd.Name(), args)
		wd.App.SysLog.WithFields(map[string]interface{}{
			"trace_id": ctx.Value("trace_id"),
			"type":     "command",
		}).Info(info)
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		info := fmt.Sprintf("end %s args:%+v", cmd.Name(), args)
		wd.App.SysLog.WithFields(map[string]interface{}{
			"trace_id": ctx.Value("trace_id"),
			"type":     "command",
		}).Info(info)
	},
}

func init() {
	ctx = context.WithValue(context.Background(), "trace_id", wd.GetTraceID())
	var (
		demo = commandDemo.NewCommand()
	)
	rootCmd.AddCommand(demo.Demo(ctx), demo.Demo2(ctx))
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
